package cn.icare.cms;


import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ExcelWriter<T> {

    public XSSFWorkbook exportData(String title, String[] headers,String[] colunms, List<T> dataList) {
        //创建工作簿，对应整个xlsx文件
        XSSFWorkbook workbook = new XSSFWorkbook();

        //创建sheet，对应excel的单个sheet
        XSSFSheet sheet = workbook.createSheet("sheet1");
        //设置行高、列宽为自适应（应用于指定数据范围）
        sheet.autoSizeColumn(1,true);


        //#region 1-创建表格标题，主要特点是：在第一行、居中、独占一个单元格成为一行（合并等表宽的行部分）等等。
        //1.1 第一步合并单元格
        CellRangeAddress cellAddresses = new CellRangeAddress(0, 0, 0, headers.length - 1);
        sheet.addMergedRegion(cellAddresses);
        //1.2 第二步：创建样式（居中、字体）
        XSSFCellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        Font font = new XSSFFont();
        font.setBold(true);
        font.setFontName("黑体");
        font.setFontHeightInPoints((short) 18);//设置字体大小
        headerStyle.setFont(font);
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        //设置表格标题内容
        cell.setCellValue(title);
        //设置单元格样式
        cell.setCellStyle(headerStyle);
        //#endregion

        //#region 2-创建表格表头
//        设计表头样式
        XSSFCellStyle tbHeadStyle = workbook.createCellStyle();
        tbHeadStyle.setAlignment(HorizontalAlignment.CENTER); //水平居中
        tbHeadStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直居中
        Font fontTbHead = new XSSFFont();
        fontTbHead.setBold(true);
        fontTbHead.setFontName("黑体");
        fontTbHead.setFontHeightInPoints((short) 16);//设置字体大小
        tbHeadStyle.setFont(fontTbHead);
        tbHeadStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(235,237,239)));// 设置背景色
        tbHeadStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        XSSFRow tbHeadRow = sheet.createRow(1); //第二行

        XSSFCell tbHeadCell;
        for (int i = 0; i < headers.length; i++) {
            tbHeadCell = tbHeadRow.createCell(i);
            tbHeadCell.setCellStyle(tbHeadStyle);
            tbHeadCell.setCellValue(headers[i]);
        }
        sheet.setColumnWidth(0, 7000);
        sheet.setColumnWidth(1, 3766);
        //#endregion

        //#region 3-创建表格数据
        //创建数据单元格样式对象

        XSSFCellStyle tbDataStyle = workbook.createCellStyle();

        //设置数据单元格样式:内容水平居中

        tbDataStyle.setAlignment(HorizontalAlignment.CENTER);

        //设置数据单元格样式:内容垂直居中

        tbDataStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        int dataBeginRow = 2;
        XSSFRow tableDataRow = null;
        XSSFCell tableDataCell = null;
        for (T model : dataList) {
            tableDataRow = sheet.createRow(dataBeginRow);
            if (model instanceof Map) {
                Map<String, Object> map = (Map<String, Object>) model;
                int i = 0;
                for (String key:colunms){
                    Object value = map.get(key);
                    tableDataCell = tableDataRow.createCell(i);
                    //设置单元格数据
                    tableDataCell.setCellValue(value.toString());
                    tableDataCell.setCellStyle(tbDataStyle);
                    i++;
                }
            } else {
                Field[] fields = model.getClass().getDeclaredFields();
                if (fields != null && fields.length > 0) {

                    for (int i = 0; i < fields.length; i++) {

                        try {

                            Field f = fields[i];
                            //设置私有变量可操作

                            f.setAccessible(true);

                            //获取字段的值

                            String value = f.get(model).toString();

                            tableDataCell = tableDataRow.createCell(i);

                            //设置单元格数据

                            tableDataCell.setCellValue(value);

                            tableDataCell.setCellStyle(tbDataStyle);

                        } catch (Exception e) {

                            e.printStackTrace();

                        }

                    }
                }
            }
            dataBeginRow++;
        }

        //#endregion

        return workbook;
    }
}
